import React, {useState, useRef} from 'react'
import {message, Divider, Popconfirm} from 'antd'
import {PageHeaderWrapper} from '@ant-design/pro-layout'
import ProTable, {ProColumns, ActionType} from '@ant-design/pro-table'
import {connect} from 'umi'
import {getCredentialList, saveOrUpdateCredential, deleteCredential} from '../service'
import moment from 'moment'
import CreateForm from './components/CreateForm'

const TableList = () => {
  const actionRef = useRef();
  const [visible, setVisible] = useState(false);
  const [record, setRecord] = useState(null);
  const [operateType, setOperateType] = useState(null);

  const handleCancel = () => {
    setVisible(false);
    setRecord(null);
  };

  const handleSubmit = (values) => {
    saveOrUpdateCredential(values).then(res => {
      if (res.flag){
        message.success(`修改成功！`)
      }else {
        message.error(res.error);
      }
      setVisible(false);
      actionRef.current.reload();
    })
  };

  const queryDetails = (record,type) => {
    setOperateType(type);
    setVisible(true);
    setRecord(record)
  };

  const delCredential = (id) => {
    deleteCredential(id).then(res => {
      if (res.flag){
        message.success('资质删除成功！')
      }else {
        message.error(res.error)
      }
      actionRef.current.reload();
    })
  };

  const columns = [
    {
      title: '序号',
      render: (text, record, index) => `${index + 1}`,
      search:false,
    },
    {
      title: '姓名',
      dataIndex: 'name',
    },
    {
      title: '公司',
      dataIndex: 'orgName',
      search:false,
    },
    {
      title: '部门',
      dataIndex: 'unitName',
      search:false,
    },
    {
      title: '岗位',
      dataIndex: 'jobName',
      search:false,
    },
    {
      title: '职称系列',
      search:false,
      dataIndex: 'jobSeries',
    },
    {
      title: '证书级别',
      search:false,
      dataIndex: 'credentialLevel',
    },
    {
      title: '资格名称',
      search:false,
      dataIndex: 'credentialName',
    },
    {
      title: '授予时间',
      dataIndex: 'startTime',
      search:false,
      render: (text) => {
        return moment(text).format('YYYY-MM-DD');
      },
    },
    {
      title: '到期时间',
      dataIndex: 'endTime',
      search:false,
      render: (text) => {
        return moment(text).format('YYYY-MM-DD');
      },
    },
    {
      title: '是否年审',
      search:false,
      dataIndex: 'isAudit',
      valueEnum: {
        0: {text: '否', status: 'Default'},
        1: {text: '是', status: 'Success'},
      },
    },
    {
      title: '挂靠状态',
      search:false,
      dataIndex: 'hangStatus',
      valueEnum: {
        0: {text: '未审核', status: 'Default'},
        1: {text: '审核中', status: 'Processing'},
        2: {text: '已挂靠', status: 'Success'},
        4: {text: '解除中', status: 'Processing'},
        5: {text: '已解除', status: 'Error'},
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (<>
          <a onClick={() => queryDetails(record, 2)}>详情</a>
          <Divider type="vertical"/>
          <a onClick={() => queryDetails(record, 1)}>修改</a>
          <Divider type="vertical"/>
          {(record.hangStatus!==0 && record.bpmUrl) && <><a onClick={() => window.open(record.bpmUrl)}>审批详情</a></>}
          {/*<Divider type="vertical"/>
          <Popconfirm title="是否要删除此数据？" cancelText='取消' okText='确认' onConfirm={() => delCredential(record.id)}>
            <a>删除</a>
          </Popconfirm>*/}</>
      ),
    }
  ];

  return (
    <PageHeaderWrapper>
      <ProTable
        actionRef={actionRef}
        rowKey="id"
        request={(params) => getCredentialList(params,[1])}
        columns={columns}
      />
      <CreateForm
        operateType={operateType}
        visible={visible}
        record={record}
        onCancel={handleCancel}
        onSubmit={handleSubmit}
      />

    </PageHeaderWrapper>
  )
}

export default connect(() => ({}))(TableList)
